package com.oocl.servermanager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import com.oocl.constant.ProtocolType;
import com.oocl.operation.OnlineUserDao;
import com.oocl.operation.impl.OnlineUserDaoImpl;
import com.oocl.pojo.Protocol;
import com.oocl.server.ChatServer;
import com.oocl.util.JsonUtil;
import com.oocl.util.ResourceCloser;

public class ManagerThread extends Thread{
	private ChatServer server;
	private Socket socket;
	private BufferedReader reader;
	private PrintWriter writer;
	private ServerManager manager;
	
	public ManagerThread(ChatServer server,Socket socket,ServerManager manager) {
		this.server = server;
		this.manager = manager;
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			writer = new PrintWriter(socket.getOutputStream());
			
			String line = reader.readLine();
			System.out.println(line);
			System.out.println("server="+server);
			if(line==null){
				return;
			}
			Protocol protocol= JsonUtil.toProtocol(new JSONObject(line.trim()));
			switch (protocol.getType()) {
			case ProtocolType.START_CHAT:
				if(server==null){
					server = new ChatServer();
					new Thread(){

						@Override
						public void run() {
							server.start();
						}
						
					}.start();
					protocol.setContent("server start success!");
					manager.setCurrChatServer(server);
				}else {
					protocol.setType(ProtocolType.ERROR);
					protocol.setContent("server already start");
					protocol.setTime(new Date());
				}
				send(writer, protocol);
				break;
			case ProtocolType.STOP_CHAT:
				if(server==null){
					protocol.setType(ProtocolType.ERROR);
					protocol.setContent("server already end");
					protocol.setTime(new Date());
				}else {
					server.close();
					protocol.setContent("server stop success!");
					manager.setCurrChatServer(null);
				}
				send(writer, protocol);
				break;
			case ProtocolType.GET_CHAT_TIME:
				if(server==null){
					protocol.setType(ProtocolType.ERROR);
					protocol.setContent("server not start!");
				}else {
					protocol.setContent(server.getRunningTime()+"");
				}
				protocol.setTime(new Date());
				send(writer, protocol);
				break;
			case ProtocolType.LOAD_USERS:
				if(server==null){
					protocol.setType(ProtocolType.ERROR);
					protocol.setContent("server not start!");
				}else {
					OnlineUserDao userDao = new OnlineUserDaoImpl();
					ArrayList<String> list = userDao.findAll();
					protocol.setOnlineUsers(list);
				}
				protocol.setTime(new Date());
				send(writer, protocol);
				break;
			default:
				break;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			ResourceCloser.release(writer, reader, socket, null);
		}
	}
	
	private void send(PrintWriter out,Protocol protocol){
		out.println(JsonUtil.toJsonObject(protocol).toString());
		out.flush();
	}
	
}
